//
//  HomeTopView.m
//  Learn
//
//  Created by cjg.mini on 2024/3/29.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "HomeTopView.h"
@interface HomeTopView ()<UITextFieldDelegate>

@end

@implementation HomeTopView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self setupUI];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(searchAction:)];
        [self.bgView addGestureRecognizer:tap];
    }
    return self;
}

- (void)setupUI {
    self.bgView = [[UIView alloc] init];
    self.bgView.backgroundColor = COLOR_WHITE;
    self.bgView.layer.cornerRadius = 20;
    self.bgView.layer.masksToBounds = YES;
    [self addSubview:self.bgView];
    
    self.searchImBtn = [[UIImageView alloc] init];
    [self.searchImBtn setImage:[UIImage imageNamed:@"icon_search"]];
    [self addSubview:self.searchImBtn];
    
    self.textFD = [[UILabel alloc] init];
    self.textFD.text = @"请输入搜索内容";
    self.textFD.font = [UIFont systemFontOfSize:20];
    self.textFD.textColor = COLOR_b5b5b5;
    [self addSubview:self.textFD];
    
    self.searchBtn = [[UILabel alloc] init];
    self.searchBtn.text = @"搜索";
    self.searchBtn.textColor = COLOR_WHITE;
    self.searchBtn.textAlignment = NSTextAlignmentCenter;
    self.searchBtn.backgroundColor = rgbHex(0x09CC76, 1);
    self.searchBtn.layer.cornerRadius = 18;
    self.searchBtn.layer.masksToBounds = YES;
    [self addSubview:self.searchBtn];
    
    self.mesBtn = [[UIButton alloc] init];
    [self.mesBtn setImage:[UIImage imageNamed:@"icon_message"] forState:(UIControlStateNormal)];
    [self addSubview:self.mesBtn];
    [self.mesBtn addTarget:self action:@selector(mesAction:) forControlEvents:(UIControlEventTouchUpInside)];

    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(0.0);
        make.left.equalTo(self).offset(10.0);
        make.right.equalTo(self).offset(-60);
        make.height.mas_equalTo(40);
    }];
    [self.searchImBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(10.0);
        make.centerY.mas_equalTo(self.bgView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(16, 16));
    }];
    [self.textFD mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.bgView.mas_centerY);
        make.left.equalTo(self.searchImBtn.mas_right).offset(10.0);
        make.height.mas_equalTo(40);
    }];
    [self.searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView).offset(2.0);
        make.right.bottom.equalTo(self.bgView).offset(-2);
        make.width.mas_equalTo(64);
    }];
    [self.mesBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_right).offset(20.0);
        make.centerY.mas_equalTo(self.bgView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(26, 26));
    }];

}

- (void)mesAction:(UIButton *)sender {
    if (self.messageBlock) {
        self.messageBlock();
    }
}

- (void)searchAction:(UITapGestureRecognizer *)sender {
    if (self.searchBlock) {
        self.searchBlock();
    }
}

@end
